package com.wing.res.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.common.enums.MaintenanceEnum;
import com.wing.common.model.entity.ExternalProduct;
import com.wing.common.service.ExternalProductService;
import com.wing.res.dao.ResProEchnologyParamDao;
import com.wing.res.model.entity.ResProEchnologyParam;

import com.wing.res.model.request.ProcessProjectData;
import com.wing.res.model.response.ResProTaskRecordSearchVo;
import com.wing.res.service.ResProEchnologyParamService;
import com.wing.sell.model.entity.CustomersInfo;
import com.wing.sell.model.entity.OrderProEnclosure;
import com.wing.sell.service.CustomersInfoService;
import com.wing.sell.service.OrderProEnclosureService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;


/**
 * 产品工艺路线表-工序-ServiceImpl
 * @version 1.1.0
 * @Author hz
 * @Date 2024-09-23 21:32
 **/
@Service
public class ResProEchnologyParamServiceImpl extends ServiceImpl<ResProEchnologyParamDao, ResProEchnologyParam> implements ResProEchnologyParamService {

    @Autowired
    private CustomersInfoService customersInfoService;

    @Autowired
    private ExternalProductService externalProductService;

    @Autowired
    private OrderProEnclosureService orderProEnclosureService;

    /**
     * 获取老版本的信息
     *
     * @param skuCode
     * @return
     */
    @Override
    public List<ProcessProjectData> getOldVersionInfo(Long taskId) {
        return this.baseMapper.getOldVersionInfoById(taskId);
    }

    /**
     * 根据物料id查询出对应的工序
     * @param taskId
     * @return
     */
    @Override
    public List<ProcessProjectData> getOldVersionInfoById(Long taskId) {
        return this.baseMapper.getOldVersionInfoById(taskId);
    }

    @Override
    public List<ResProEchnologyParam> list(ResProEchnologyParam echnologyParam) {
        //HEZHOUTODU
        QueryWrapper<ResProEchnologyParam> queryWrapper = new QueryWrapper<ResProEchnologyParam>();
//        if (echnologyParam.getProductDetailId() != null) {
//            queryWrapper.eq("product_detail_id", echnologyParam.getProductDetailId());
//        }
//        if (StringUtils.isNotBlank(echnologyParam())) {
//            queryWrapper.eq("sku_code", echnologyParam.getSkuCode());
//        }
        queryWrapper.orderByAsc("sorted");
        return this.baseMapper.selectList(queryWrapper);
    }

    /**
     * 查询个数
     * @param taskId
     * @return
     */
    @Override
    public Integer countVersion(Long taskId) {
        return this.baseMapper.countVersion(taskId);
    }

    /**
     * 查询工艺规划
     * @param pg
     * @param map
     * @return
     */
    @Override
    public Page<ResProTaskRecordSearchVo> pageList4app(Page<ResProTaskRecordSearchVo> pg, Map<String, Object> map) {

        List<ResProTaskRecordSearchVo> tasks = this.baseMapper.pageList4app(pg,map);

        tasks.forEach(info->{
            //如果类型是 客户订单  则需要查询客户信息
            if (info.getSource().equals(MaintenanceEnum.Task_Source_Enum.KHDD.getCode())){
                //客户
                CustomersInfo byId = customersInfoService.getById(info.getCustomerId());
                if (ObjectUtil.isNotEmpty(byId)){
                    info.setCustomerCode(byId.getCustomerName());
                }
                ExternalProduct externalProduct = externalProductService.getById(info.getExtProductId());
                if (ObjectUtil.isNotEmpty(externalProduct)){
                    info.setCusProductName(externalProduct.getName());
                    info.setCusProductCode(externalProduct.getCode());
                }

                QueryWrapper<OrderProEnclosure> enclosureQueryWrapper = new QueryWrapper<>();
                enclosureQueryWrapper.eq("task_id",info.getId());
                OrderProEnclosure one = orderProEnclosureService.getOne(enclosureQueryWrapper);
                if (ObjectUtil.isNotEmpty(one)){
                    info.setFileUrl(one.getFileUrl());
                    info.setCusProductFigure(one.getFileName());
                }
            }
        });
        pg.setRecords(tasks);
        return pg;
    }


    /**
     * 根据任务id和recordId查询版本
     * @param recordId
     * @return
     */
    @Override
    public List<ProcessProjectData> getVersionInfoBy( String recordId) {
        return this.baseMapper.getVersionInfoBy(recordId);
    }
}
